package com.zretc.support.service.serviceimpl;

import com.zretc.support.dao.TopicsingleDao;
import com.zretc.support.entity.Topicsingle;
import com.zretc.support.service.TopicsingleService;
import com.zretc.support.tool.RandomTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TopicsingleServiceImpl implements TopicsingleService {
    @Autowired
    TopicsingleDao topicsingleDao;

    @Override
    public List<Topicsingle> getAllTopicsingle() {
        return topicsingleDao.findAll();
    }

    /**
     * 添加
     * @param topicsingle
     * @return
     */
    @Override
    public Topicsingle addTopicsingle(Topicsingle topicsingle) {
        RandomTopic randomTopic = new RandomTopic();
        String topicIdA = "A"+randomTopic.getRandom();
        Topicsingle Test = null;
        try{
            Test = topicsingleDao.findByTopicId(topicIdA);
        }catch (NullPointerException e){

        }
        //判断随机生成的topocId在数据库中是否存在
        if (Test == null){
            topicsingle.setTopicId(topicIdA);
            topicsingle = topicsingleDao.save(topicsingle);
        }else{
            addTopicsingle(topicsingle);
        }
        return topicsingle;
    }

    /**
     * 修改
     * @param topicsingle
     * @return
     */
    @Override
    public int updTopicsingle(Topicsingle topicsingle) {
        topicsingleDao.save(topicsingle);
        return 1;
    }

    /**
     * 删除
     * @param topicId
     * @return
     */
    @Override
    public int deleteTopicsingle(String topicId) {
        return topicsingleDao.deleteByTopicId(topicId);
    }

    @Override
    public List<Topicsingle> selectTopicsingleByTopicName(String topicName) {
        return topicsingleDao.getAllByTopicName(topicName);
    }

    @Override
    public Page<Topicsingle> getTopicsingleByPage(String keywords,String level,String type, int page, int pageSize) {
        Pageable pageable = PageRequest.of(page-1,pageSize);
        return topicsingleDao.getAllTopicsSingleByPage(keywords,level,type,pageable);
    }

    @Override
    public Page<Topicsingle> getTopicsingleByCourseIdWithPage(List<Long> courseId, String level, String type, int page, int pageSize) {
        Pageable pageable = PageRequest.of(page-1,pageSize);
        return topicsingleDao.getTopicsingleByCourseIdWithPage(courseId,level,type,pageable);
    }

    @Override
    public Page<Topicsingle> getTopicsingleByRand(int course,int num, int type) {
        Pageable pageable = PageRequest.of(0,num);
        Page<Topicsingle> page = null;
        if(type == 1){
            page = topicsingleDao.getRandomJudege(course,pageable);
        }else if(type == 2){
            page = topicsingleDao.getRandomInsert(course,pageable);
        }else if(type == 3){
            page = topicsingleDao.getRandomObject(course,pageable);
        }
        return page;
    }

    @Override
    public Topicsingle getTopicsingleByTopicId(String topicId) {
        return topicsingleDao.findByTopicId(topicId);
    }

    @Override
    public Page<Topicsingle> getsingleByIdWithPage(String keywords, int page, int pageSize) {
        Pageable pageable = PageRequest.of(page,pageSize);
        return topicsingleDao.getAllByTopicId(keywords,pageable);
    }
}
